<?php

/*
 * 默认控制器
 * @author  kane.xie  mr.ziyong@qq.com
 * @time 2010.5.9
 */

class IndexAction extends Action {

    public function index() {
        if (isset($_GET['id'])) {
            $common = KM('Common');
            $sel_sql = "select * from tq_url where `seid`='" . $_GET['id'] . "'";
            $sel_data = $common->selectBS($sel_sql);
            if ($sel_data) {
                header('Location:' . $sel_data['url']);
            } else {
                header('Location:./');
            }
        } else {
            $this->display('index');
        }
    }

    //步骤一
    public function k_step1() {
        if (isset($_GET['link'])) {
            $itemid = $this->getItemId($_GET['link']);
            if (!empty($itemid)) {
                $taobao = new Taobao;
                $data = $taobao->getItem($itemid);
                if ($data) {
                    $data['itemurl'] = 'http://item.taobao.com/item.htm?id=' . $itemid;
                    $data['commission_rate'] = sprintf("%01.2f", $data['commission_rate'] / 100);
                    $this->assign('data', $data);
                    $distemp = 'step1';
                } else {
                    $msg = "该商品没有折扣";
                    $this->assign('msg', $msg);
                    $distemp = 'index';
                }
            } else {
                $msg = "商品链接有误,请检查重新输入您的商品链接";
                $this->assign('msg', $msg);
                $distemp = 'index';
            }
        } else {
            $msg = "请输入商品链接";
            $distemp = 'index'; //没有输入链接,回到首页并提示输入商品
        }
        $this->display($distemp);
    }

    //步骤二
    public function k_step2() {
        if (isset($_POST['tbusername']) && isset($_POST['itemid'])) {
            $taobao = new Taobao;
            $data = $taobao->getItem($_POST['itemid'], $_POST['tbusername']);
            if ($data) {
                $id = substr(md5($_POST['itemid'] . $_POST['tbusername']), 3, 8);
                $myurl = KC('site') . "/" . $id;
                $common = KM('Common');
                $sel_sql = "select * from tq_url where `seid`='" . $id . "'";
                $sel_data = $common->selectBS($sel_sql);
                if (!$sel_data) {
                    $sql = "insert into tq_url set `seid`='" . $id . "',`url`='" . $data['click_url'] . "',`nick`='" . $_POST['tbusername'] . "',`intime`=" . time();
                    $common->addBS($sql);
                }
                echo "成功生成链接,点击购买:<a href='{$myurl}'>{$myurl}</a>";
            } else {
                echo "淘宝会员名无效";
            }
        } else {
            echo "请确认你的淘宝会员名";
        }
    }

    /*
     * 根据url获取商品Id
     * @param $url string
     */

    public function getItemId($url) {
        $itemid = '';
        $idname=array('id','default_item_id','mallstItemId');
        if (!empty($url)) {
            $info = parse_url($url);
            if (!empty($info['query'])) {
                $query_vars = explode("&", $info['query']);
                foreach ((array) $query_vars as $k => $v) {
                    $vars = explode("=", $v);
                    if (in_array($vars[0], $idname)) {
                        $itemid = $vars[1];
                    }
                }
            }
        }
        return $itemid;
    }

}

?>